package com.example.backend.controller;

import com.example.backend.entity.Vehicle;
import com.example.backend.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/vehicle")//标记控制器
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;
    @GetMapping("/list")
    public List<Vehicle> findAll() {
        return vehicleService.findAll();
    }
    @PostMapping("/save")
    public Vehicle saveVehicle(@RequestBody Vehicle vehicle){
        return vehicleService.save(vehicle);
    }
    @GetMapping("/delete")
    public void delete(@Param("id") Integer id){
        vehicleService.delete(id);
    }
    @PostMapping("/update")
    public Vehicle update(@RequestBody Vehicle vehicle){
        return vehicleService.update(vehicle);
    }
    @GetMapping("/repeat")
    public boolean findName(@Param("name") String name){
        System.out.println(name);
        return vehicleService.findName(name);
    }

    @GetMapping("/changeRepeat")
    public boolean findNameAndId(@Param("id") Integer id,@Param("name") String name){
        System.out.println(name);
        return vehicleService.findNameAndId(id,name);
    }
    @GetMapping("/search")
    public List<Vehicle> findVehicleByName(@Param("key") String key,@Param("value") String value) throws ParseException{
        System.out.println(key);
        System.out.println(value);
        switch (key){
            case "id":
                return vehicleService.findById(Integer.parseInt(value));
            case "vehicleCategory":
                return vehicleService.findByVehicleCategory(value);
            case "name":
                return vehicleService.findByName(value);
            case "buyDate":
                return vehicleService.findByBuyDate(value);
            case "status":
                return vehicleService.findByStatus(value);
            case "rent":
                return vehicleService.findByRent(Integer.parseInt(value));
            case "licenseNumber":
                return vehicleService.findByLicenseNumber(value);
            default:break;
        }
        return null;
    }
}
